import logging

from flask import Blueprint, request
from flask import jsonify

from airflow.patsnap.service import user_service
from airflow.exceptions import AirflowException
from airflow.models.idata_user import User
from airflow.utils.session import create_session
from airflow.www.api import provide_user

log = logging.getLogger(__name__)

api_speech_user = Blueprint('api_speech_user', __name__)


# user route
@api_speech_user.route('/users/<int:id>', methods=['GET'])
def get_user(id):
    try:
        with create_session() as session:
            user = User.get_user(id, session)
            return jsonify(code=0, message='ok', data=User.to_json(user))
    except AirflowException as e:
        log.error("get_user error =%s, params=%s", e, id, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_user.route('/users', methods=['GET'])
def get_users():
    try:
        page = request.args.get('page', 0, type=int)
        result = user_service.get_users(page)
        return jsonify(code=0, message='ok', data=result)
    except AirflowException as e:
        log.error("get_users error =%s", e, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_user.route('/users', methods=['POST'])
@provide_user
def add_user(oa_user_name):
    try:
        user_name = request.args.get("user_name", type=str, default=None)
        add_oa_user_name = request.args.get("oa_user_name", type=str, default=None)
        department_name = request.args.get("department_name", type=str, default=None)
        role_ids = request.json

        if user_name is None or oa_user_name is None or department_name is None:
            log.error("add_user params error user_name={%s},oa_user_name={%s},department_name={%s},",
                      user_name, add_oa_user_name, department_name)
            return jsonify(code=400, message="add_user params has None,", data=None)
        user_service.add_user(user_name, add_oa_user_name, department_name, oa_user_name, role_ids)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("add_user error =%s", e)
        return jsonify(code=500, message=str(e), data=None)


@api_speech_user.route('/users/<int:id>', methods=['DELETE'])
def delete_user(id):
    try:
        oa_user_name = request.args.get("oa_user_name", type=str, default=None)
        if oa_user_name is None:
            log.error("delete_user params error oa_user_name={%s}", oa_user_name)
            return jsonify(code=400, message="delete_user params has None,", data=None)
        user_service.delete_user(id, oa_user_name)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("delete_user error =%s", e)
        return jsonify(code=500, message=str(e), data=None)


@api_speech_user.route('/users', methods=['PUT'])
@provide_user
def update_user(oa_user_name):
    try:
        id = request.args.get("id", type=int, default=None)
        user_name = request.args.get("user_name", type=str, default=None)
        add_oa_user_name = request.args.get("oa_user_name", type=str, default=None)
        department_name = request.args.get("department_name", type=str, default=None)
        role_ids = request.json

        if user_name is None or oa_user_name is None or department_name is None:
            log.error(
                "update_user params error id={%s},user_name={%s},oa_user_name={%s},department_name={%s}",
                id, user_name, oa_user_name, department_name)
            return jsonify(code=400, message="update_user params has None,", data=None)
        user_service.update_user(id, user_name, add_oa_user_name, department_name, oa_user_name, role_ids)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("update_user error =%s", e)
        return jsonify(code=500, message=str(e), data=None)


# 获取所有项目
@api_speech_user.route('/users/projects', methods=['GET'])
@provide_user
def get_user_projects(oa_user_name):
    try:
        page = request.args.get('page', 0, type=int)

        result = user_service.get_user_projects(page, oa_user_name)
        return jsonify(code=0, message='ok', data=result)
    except AirflowException as e:
        log.error("get_user_projects error =%s", e, )
        return jsonify(code=500, message=str(e), data=None)
